package cn.itsource.shop.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.EmailService;
import cn.itsource.basic.util.RedisService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.shop.domain.Shop;
import cn.itsource.shop.mapper.ShopMapper;
import cn.itsource.shop.query.ShopQuery;
import cn.itsource.shop.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 万能键 alt + enter
 */
@Service //交给spring管理
public class ShopServiceImpl extends BaseServiceImpl<Shop, ShopQuery> implements IShopService {

    /**
     * 注入mapper层对象
     */
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private EmailService emailService;


    @Override
    @Transactional
    public void settlement(Shop shop) {
        /**
         * 先写主体,再写细节
         *  //1.验证两次输入密码一致
         *  //2.验证账号不能重复  sql
         *
         * 主体
         *    1.新增员工数据,拿到员工的主键id
         *    2.把员工id设置给店铺的adminId,新增店铺,拿到店铺的id
         *    3.店铺id设置给员工的shop_id,修改员工信息
         *
         * 细节
         *   1.两次密码要一致
         *   2.验证参数非空 (不要相信前端,因为前端的判断post就跳过了)
         */

        //1.新增员工数据,拿到员工的主键id
        Employee admin = shop.getAdmin();

        //细节1,判断两次密码一致
        if (!StringUtils.equals(admin.getPassword(),admin.getComfirmPassword())){
            throw new RuntimeException("两次密码不一致");
        }
        //细节2.验证账号不能重复  sql
        List<Employee> employees = employeeMapper.findByUsernameOrEmailOrPhone(admin);
        if (employees.size() > 0){
            throw new RuntimeException("账号已被注册,有可能是你的手机号,邮箱,用户名其中一个和其他账号重复了");
        }

       // String password = admin.getPassword();//123
        //1.1加盐加密
//        String salt = IdUtil.fastSimpleUUID();
//        String md5Pwd = MD5.create().setSalt(salt.getBytes()).digestHex(password);
//        //2.2设置给字段
//        admin.setSalt(salt);
//        admin.setPassword(md5Pwd);

        //调用钩子方法
        admin.initPwd();

        employeeMapper.insertSelective(admin);
        Long adminId = admin.getId();

        //2.把员工id设置给店铺的adminId,新增店铺,拿到店铺的id
        shop.setAdminId(adminId);
        shopMapper.insertSelective(shop);
        Long shopId = shop.getId();

        //3.店铺id设置给员工的shop_id,修改员工信息
        admin.setShopId(shopId);
        employeeMapper.updateByPrimaryKeySelective(admin);


        //发送邮件的流程
        //1.往redis存key uuid  value:店铺id
        String uuid = IdUtil.fastSimpleUUID();
        redisService.setStringKeyAndValue(uuid,shopId,10);

        //2.发送一个邮件,邮件里面有个激活链接
        emailService.sendEmail("宠物雷院激活","请点击激活链接,http://localhost:80/shop/jihuo/"+uuid,admin.getEmail());

    }

    @Override
    public AjaxResult jihuo(String uuid) {
        //1.从redis中吧uuid做为key, 拿到之前放进去value,就是店铺的id
        String shopId = redisService.getKeyObjectValue(uuid);
        //2.如果店铺id不存在,提示用户过期了
        if (shopId == null) {
            return AjaxResult.createError("激活链接已经失效,请联系管理员");
        }

        //3.如果拿到,但是已经是激活状态了,提示一下已经激活过了
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        if (shop != null && shop.getState() == 3){
            //3.1删除redis的数据
            redisService.deleteRedisByKey(uuid);
            return AjaxResult.createError("已经激活过了,不需要重复激活");

        }

        //4.根据店铺id修改state状态
        shop.setState(3);
        shopMapper.updateByPrimaryKeySelective(shop);
        
        return AjaxResult.createSuccess("激活成功,谢谢您的认可");
    }

    public static void main(String[] args) {
        String s = MD5.create().digestHex("123456");
        System.out.println(s);//e10adc3949ba59abbe56e057f20f883e

        String s1 = IdUtil.fastSimpleUUID();
        String s2 = IdUtil.fastSimpleUUID();
        System.out.println(s1);
        System.out.println(s2);

        String s3 = MD5.create().setSalt(s1.getBytes()).digestHex("123456");
        String s4 = MD5.create().setSalt(s2.getBytes()).digestHex("123456");
        System.out.println(s3);
        System.out.println(s4);

        System.out.println("你的盐值:"+s1+",你的密码串:"+s3);
        System.out.println("行长的盐值:"+s2+"行长的密码串:"+s4);

        //数据库保存的盐 889f0c37ca5f497aabf24f1c00578ba2  123456  899e35dd92e6add6da894b4cd74401aa
        String s6 = MD5.create().setSalt("889f0c37ca5f497aabf24f1c00578ba2".getBytes()).digestHex("999666");
        System.out.println(s6);
        //数据库保存的盐 889f0c37ca5f497aabf24f1c00578ba2  123456  899e35dd92e6add6da894b4cd74401aa
        String s7 = MD5.create().setSalt("6319feba51164404ae3ac587ce846153".getBytes()).digestHex("123456");
        System.out.println(s7);

    }

}
